package com.eight.system.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclUser;
import com.eight.system.dao.AclUserDao;
import com.eight.system.service.UserService;
import com.eight.system.vo.AclUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/22 20:37
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AclUserDao aclUserDao;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public AclUser FindByNameAndPwd(String username, String password) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        //把输入的密码加盐

        wrapper.eq("password", DigestUtil.sha256Hex(password));
        AclUser aclUser = aclUserDao.selectOne(wrapper);
        return aclUser;
    }



    @Override
    public Page<AclUser> findPage(Integer currentSize, Integer pageSize, AclUserVo aclUserVo) {
        Page<AclUser> page = new Page<>(currentSize,pageSize);
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(aclUserVo.getNickName())){
            wrapper.like("nick_name",aclUserVo.getNickName());
        }
        Page<AclUser> aclUserPage = aclUserDao.selectPage(page, wrapper);
        return aclUserPage;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        AclUser user=new AclUser();
        user.setId(id);
        user.setIsDeleted(status);
        user.setGmtModified(LocalDateTime.now()); //JDK1.8---->Date线程安全问题--LocalDate LocalDateTime
        int row = aclUserDao.updateById(user);
        return row;
    }

    @Override
    public int deleteUserById(String id) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        if (id != null){
            wrapper.eq("id",id);
        }
        int delete = aclUserDao.delete(wrapper);
        return delete;
    }

    @Override
    public int updateUserByUser(AclUser aclUser) {
        aclUser.setGmtModified(LocalDateTime.now());
        int i = aclUserDao.updateById(aclUser);
        return i;
    }

    @Override
    public int addUserByUser(AclUser aclUser) {
        aclUser.setId(IdUtil.getSnowflakeNextIdStr()+"");
        aclUser.setGmtCreate(LocalDateTime.now());
        aclUser.setGmtModified(LocalDateTime.now());
        aclUser.setIsDeleted(0);
        aclUser.setPassword("8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
        aclUser.setHeadUrl("https://team11.oss-cn-beijing.aliyuncs.com/1.jpg");
        int insert = aclUserDao.insert(aclUser);
        return insert;
    }

    @Override
    public int updateUserPassword(String id, String oldPassword, String newPassword) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);
        }
        if (StringUtils.hasText(oldPassword)){
            wrapper.eq("password",DigestUtil.sha256Hex(oldPassword));
        }
        List<AclUser> users = aclUserDao.selectList(wrapper);
        if (users.size()==0){
            return 0;
        }
        AclUser aclUser = new AclUser();
        aclUser.setId(id);
        aclUser.setPassword(DigestUtil.sha256Hex(newPassword));
        aclUser.setGmtModified(LocalDateTime.now());
        aclUserDao.updateById(aclUser);
        return 1;
    }


}
